//#include "SimpleAudioEngine.h"
#include "Character.h"
#include "Map/GameMap.h"
#include "Supporting/Enums/EnumCharacterState.h"
#include "CharacterAI.h"

using namespace cocos2d;
//using namespace CocosDenshion;

//===== CONSTRUCTOR =====
CharacterAI::CharacterAI()
{
}

CharacterAI::CharacterAI(GameMap* pMap)
{
	_pMapReference = pMap;
}

//===== DESTRUCTOR =====
CharacterAI::~CharacterAI(void)
{

}

//===== PUBLIC FUNCTIONS =====
EnumCharacterState CharacterAI::getState(float timeDiff, Character* pCharReference)
{
	// Make decision based on character class
	// Set character state based on decision
	// Set character value based on decision

	switch (pCharReference->getCharState())
	{
		//STATES TO NOT MAKE A DECISION
		case RUN_STRAIGHT:
		case RUN_UP:
		case RUN_DOWN:
		case STOP:
		case DEAD:
			return pCharReference->getCharState();

		//STATES TO LOOK FOR A NEW DECISION
		case HIT_OBSTACLE:
			//if hit an obstacle, change state
			if(_pMapReference->checkCollisionWithObstacles(pCharReference->getRect(), pCharReference->getPosition()))
				return STOP;
			else
				return HIT_OBSTACLE;


		//case RUN:
		case RUN:
			//obstacle avoidance
			if (decideToHitObstacle(pCharReference))
				return HIT_OBSTACLE;

			//decide on a direction to run
			EnumCharacterState newCharacterState = _pMapReference->getNextGrid(pCharReference->getPositionY(), false);
			return newCharacterState;
	}
}


/*!
    @function decideToHitObstacle
    @param		charObstacleHitChance:int
				character's chance to hit an obstacle
    @result		decides whether the character will hit an obstacle.
    			returns true, if the randomed chance falls within the provided hit chance. also spawns an obstacle
    			returns false, if the randomed chance falls outisde the provided hit chance.
 */
bool CharacterAI::decideToHitObstacle(Character* pCharReference)
{
	int randomChance = arc4random() % 100;

	//randomChance = 100; //TODO: DEBUG, to be removed

	//if random chance falls within the character's hit chance threshold
	if (randomChance < pCharReference->getObstacleHitChance())
	{
		//create an obstacle and let the character hit it
		_pMapReference->getNextGrid(pCharReference->getPositionY(), true);
		return true;
	}
	return false;
}



